package com.ruoyi.module.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.module.domain.ExamPaper;
import com.ruoyi.module.mapper.ExamPaperMapper;
import com.ruoyi.module.mapper.ExamQuestionMapper;
import com.ruoyi.module.service.IExamPaperService;

/**
 * 试卷Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class ExamPaperServiceImpl implements IExamPaperService
{
    @Autowired
    private ExamPaperMapper examPaperMapper;

    @Autowired
    private ExamQuestionMapper examQuestionMapper;

    /**
     * 查询试卷列表
     * 
     * @param examPaper 试卷
     * @return 试卷
     */
    @Override
    public List<ExamPaper> selectExamPaperList(ExamPaper examPaper)
    {
        return examPaperMapper.selectExamPaperList(examPaper);
    }

    /**
     * 根据ID查询试卷详情
     * 
     * @param id 试卷ID
     * @return 试卷
     */
    @Override
    public ExamPaper selectExamPaperById(Long id)
    {
        return examPaperMapper.selectExamPaperById(id);
    }

    /**
     * 新增试卷
     * 
     * @param examPaper 试卷
     * @return 结果
     */
    @Override
    @Transactional
    public int insertExamPaper(ExamPaper examPaper)
    {
        return examPaperMapper.insertExamPaper(examPaper);
    }

    /**
     * 修改试卷
     * 
     * @param examPaper 试卷
     * @return 结果
     */
    @Override
    @Transactional
    public int updateExamPaper(ExamPaper examPaper)
    {
        return examPaperMapper.updateExamPaper(examPaper);
    }

    /**
     * 批量删除试卷
     * 
     * @param ids 需要删除的试卷ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteExamPaperByIds(Long[] ids)
    {
        // 删除试卷时，同时删除关联的试题
        for (Long id : ids)
        {
            examQuestionMapper.deleteExamQuestionByPaperId(id);
        }
        return examPaperMapper.deleteExamPaperByIds(ids);
    }

    /**
     * 删除试卷信息
     * 
     * @param id 试卷ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteExamPaperById(Long id)
    {
        // 删除试卷时，同时删除关联的试题
        examQuestionMapper.deleteExamQuestionByPaperId(id);
        return examPaperMapper.deleteExamPaperById(id);
    }
}

